/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CalculadoraMatrizes.java
 *
 * Created on 26/08/2009, 18:51:44
 */

package AlgebraLinear;

import javax.swing.JOptionPane;

/**
 *
 * @author Andre
 */
public class CalculadoraMatrizes extends javax.swing.JFrame {

    /** Creates new form CalculadoraMatrizes */
    public CalculadoraMatrizes() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnDetInv = new javax.swing.JPanel();
        sPn2 = new javax.swing.JScrollPane();
        txtAreaMatrizA1 = new javax.swing.JTextArea();
        cbOperacao1 = new javax.swing.JComboBox();
        btnCalcularDetInv = new javax.swing.JButton();
        lblIgual1 = new javax.swing.JLabel();
        sPn4 = new javax.swing.JScrollPane();
        txtResposta1 = new javax.swing.JTextArea();
        pnOpBasicas = new javax.swing.JPanel();
        sPn01 = new javax.swing.JScrollPane();
        txtAreaMatrizA = new javax.swing.JTextArea();
        sPn02 = new javax.swing.JScrollPane();
        txtAreaMatrizB = new javax.swing.JTextArea();
        cbOperacao = new javax.swing.JComboBox();
        btnCalcular = new javax.swing.JButton();
        lblIgual = new javax.swing.JLabel();
        sPn03 = new javax.swing.JScrollPane();
        txtResposta = new javax.swing.JTextArea();
        menuBar = new javax.swing.JMenuBar();
        menu = new javax.swing.JMenu();
        itemOpBasicas = new javax.swing.JMenuItem();
        itemDetInv = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        sPn2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        txtAreaMatrizA1.setColumns(20);
        txtAreaMatrizA1.setRows(5);
        sPn2.setViewportView(txtAreaMatrizA1);

        cbOperacao1.setFont(new java.awt.Font("Consolas", 1, 12));
        cbOperacao1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Determinante", "Inversa" }));

        btnCalcularDetInv.setText("Calcular");
        btnCalcularDetInv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalcularDetInvActionPerformed(evt);
            }
        });

        lblIgual1.setFont(new java.awt.Font("Consolas", 1, 14));
        lblIgual1.setText("=");

        txtResposta1.setColumns(20);
        txtResposta1.setRows(5);
        sPn4.setViewportView(txtResposta1);

        javax.swing.GroupLayout pnDetInvLayout = new javax.swing.GroupLayout(pnDetInv);
        pnDetInv.setLayout(pnDetInvLayout);
        pnDetInvLayout.setHorizontalGroup(
            pnDetInvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDetInvLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sPn2, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnDetInvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnDetInvLayout.createSequentialGroup()
                        .addComponent(cbOperacao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblIgual1))
                    .addComponent(btnCalcularDetInv, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(sPn4, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );
        pnDetInvLayout.setVerticalGroup(
            pnDetInvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDetInvLayout.createSequentialGroup()
                .addGroup(pnDetInvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnDetInvLayout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addGroup(pnDetInvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbOperacao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblIgual1))
                        .addGap(27, 27, 27)
                        .addComponent(btnCalcularDetInv, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnDetInvLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(pnDetInvLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(sPn2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                            .addComponent(sPn4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(32, 32, 32))
        );

        sPn01.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        txtAreaMatrizA.setColumns(20);
        txtAreaMatrizA.setRows(5);
        sPn01.setViewportView(txtAreaMatrizA);

        sPn02.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        txtAreaMatrizB.setColumns(20);
        txtAreaMatrizB.setRows(5);
        sPn02.setViewportView(txtAreaMatrizB);

        cbOperacao.setFont(new java.awt.Font("Consolas", 1, 12));
        cbOperacao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "+", "-", "*E", "*" }));

        btnCalcular.setText("Calcular");
        btnCalcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalcularActionPerformed(evt);
            }
        });

        lblIgual.setFont(new java.awt.Font("Consolas", 1, 14));
        lblIgual.setText("=");

        sPn03.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        txtResposta.setColumns(20);
        txtResposta.setRows(5);
        sPn03.setViewportView(txtResposta);

        javax.swing.GroupLayout pnOpBasicasLayout = new javax.swing.GroupLayout(pnOpBasicas);
        pnOpBasicas.setLayout(pnOpBasicasLayout);
        pnOpBasicasLayout.setHorizontalGroup(
            pnOpBasicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnOpBasicasLayout.createSequentialGroup()
                .addGroup(pnOpBasicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnOpBasicasLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(sPn01, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cbOperacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(sPn02, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnOpBasicasLayout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(btnCalcular, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblIgual)
                .addGap(18, 18, 18)
                .addComponent(sPn03, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnOpBasicasLayout.setVerticalGroup(
            pnOpBasicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnOpBasicasLayout.createSequentialGroup()
                .addGroup(pnOpBasicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnOpBasicasLayout.createSequentialGroup()
                        .addGroup(pnOpBasicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnOpBasicasLayout.createSequentialGroup()
                                .addGap(104, 104, 104)
                                .addComponent(cbOperacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnOpBasicasLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnOpBasicasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(sPn02, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                                    .addComponent(sPn01, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                                    .addComponent(sPn03, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))))
                        .addGap(18, 18, 18)
                        .addComponent(btnCalcular, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnOpBasicasLayout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(lblIgual)))
                .addContainerGap())
        );

        menu.setText("Modulo");

        itemOpBasicas.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        itemOpBasicas.setText("Operações Basicas");
        itemOpBasicas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemOpBasicasActionPerformed(evt);
            }
        });
        menu.add(itemOpBasicas);

        itemDetInv.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        itemDetInv.setText("Determinante e Inversa");
        itemDetInv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemDetInvActionPerformed(evt);
            }
        });
        menu.add(itemDetInv);

        menuBar.add(menu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnDetInv, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(pnOpBasicas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnDetInv, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(pnOpBasicas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void itemOpBasicasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemOpBasicasActionPerformed
        this.pnDetInv.setVisible(false);
        this.pnOpBasicas.setVisible(true);
    }//GEN-LAST:event_itemOpBasicasActionPerformed

    private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCalcularActionPerformed
        String[] matrizA = this.txtAreaMatrizA.getText().split("\n");
        String[] matrizB = this.txtAreaMatrizB.getText().split("\n");

        String resposta = "";
        try {
            if (this.cbOperacao.getSelectedItem().equals("+")) {
                resposta = this.controlador.calculaSoma(matrizA, matrizB);
            } else if (this.cbOperacao.getSelectedItem().equals("-")) {
                resposta = this.controlador.calculaSubtracao(matrizA, matrizB);
            } else if (this.cbOperacao.getSelectedItem().equals("*E")) {
                double escalar = 0;
                if (matrizB.length != 1) {
                    JOptionPane.showMessageDialog(rootPane, "O valor indormado deve ser um escalar");
                } else {
                    try {
                        escalar = Double.parseDouble(matrizB[0]);
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(rootPane, "O valor informado é inválido");
                    }
                    resposta = this.controlador.calculaMultiplicacaoEscalar(escalar, matrizA);
                }
            } else if (this.cbOperacao.getSelectedItem().equals("*")) {
                resposta = this.controlador.calculaMultiplicacao(matrizA, matrizB);
            }
            txtResposta.setText(resposta);
        } catch (Exception ex){
            JOptionPane.showMessageDialog(rootPane, ex.getMessage());
        }

    }//GEN-LAST:event_btnCalcularActionPerformed

    private void btnCalcularDetInvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCalcularDetInvActionPerformed
        String[] matrizA = this.txtAreaMatrizA1.getText().split("\n");

        String resposta = "";

        try{
            if(this.cbOperacao1.getSelectedItem().equals("Determinante")){
                resposta = this.controlador.calculaDeterminante(matrizA);
            }else if(this.cbOperacao1.getSelectedItem().equals("Inversa")){
                resposta = this.controlador.calculaInversa(matrizA);
            }
        } catch (Exception ex){
            JOptionPane.showMessageDialog(rootPane, ex.getMessage());
        }
        this.txtResposta1.setText(resposta);
    }//GEN-LAST:event_btnCalcularDetInvActionPerformed

    private void itemDetInvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemDetInvActionPerformed
        this.pnDetInv.setVisible(true);
        this.pnOpBasicas.setVisible(false);
    }//GEN-LAST:event_itemDetInvActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CalculadoraMatrizes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCalcular;
    private javax.swing.JButton btnCalcularDetInv;
    private javax.swing.JComboBox cbOperacao;
    private javax.swing.JComboBox cbOperacao1;
    private javax.swing.JMenuItem itemDetInv;
    private javax.swing.JMenuItem itemOpBasicas;
    private javax.swing.JLabel lblIgual;
    private javax.swing.JLabel lblIgual1;
    private javax.swing.JMenu menu;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel pnDetInv;
    private javax.swing.JPanel pnOpBasicas;
    private javax.swing.JScrollPane sPn01;
    private javax.swing.JScrollPane sPn02;
    private javax.swing.JScrollPane sPn03;
    private javax.swing.JScrollPane sPn2;
    private javax.swing.JScrollPane sPn4;
    private javax.swing.JTextArea txtAreaMatrizA;
    private javax.swing.JTextArea txtAreaMatrizA1;
    private javax.swing.JTextArea txtAreaMatrizB;
    private javax.swing.JTextArea txtResposta;
    private javax.swing.JTextArea txtResposta1;
    // End of variables declaration//GEN-END:variables

    private MatrizHandler controlador = new MatrizHandler();
}
